<template>
    <div class="app-container">
        <TableCard>
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button
                        type="primary"
                        plain
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAdd"
                        v-hasPermi="['system:dict:add']"
                        >创建
                    </el-button>
                </el-col>
            </el-row>

            <el-table
                :border="Global.tableShowBorder"
                v-loading="loading"
                :data="dataList"
                @selection-change="handleSelectionChange"
            >
                <el-table-column
                    type="index"
                    label="序号"
                    width="55"
                    align="center"
                />
                <el-table-column
                    :label="queryParams.dictName + '名称'"
                    align="center"
                    prop="dictLabel"
                >
                    <template slot-scope="scope">
                        <span
                            v-if="
                                scope.row.listClass == '' ||
                                scope.row.listClass == 'default'
                            "
                            >{{ scope.row.dictLabel }}</span
                        >
                        <el-tag
                            v-else
                            :type="
                                scope.row.listClass == 'primary'
                                    ? ''
                                    : scope.row.listClass
                            "
                        >
                            {{ scope.row.dictLabel }}
                        </el-tag>
                    </template>
                </el-table-column>

                <el-table-column
                    label="描述"
                    align="center"
                    prop="remark"
                    :show-overflow-tooltip="true"
                />

                <el-table-column
                    label="操作"
                    align="center"
                    class-name="small-padding fixed-width"
                >
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                            v-hasPermi="['system:dict:edit']"
                            >修改
                        </el-button>

                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-top"
                            @click="move(scope.$index, 'up')"
                            >上移
                        </el-button>
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-bottom"
                            @click="move(scope.$index, 'down')"
                            >下移
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

            <pagination
                v-show="total > 10"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
            />
        </TableCard>
        <!-- 添加或修改参数配置对话框 -->
        <el-dialog
            :title="title"
            :visible.sync="open"
            width="500px"
            append-to-body
        >
            <el-form
                v-if="
                    form.dictType == 'education_list' &&
                    $store.getters.allDefectObj['HR-WEB-016'] == 0
                "
                hide-required-asterisk="false"
                ref="form"
                :model="form"
                :rules="rules"
                label-width="80px"
            >
                <el-form-item>
                    <el-input
                        v-model="form.dictType"
                        :disabled="true"
                        v-show="false"
                    />
                </el-form-item>

                <el-form-item label="类别名称" prop="dictLabel">
                    <el-input
                        v-model="form.dictLabel"
                        placeholder="请输入数据标签"
                    />
                </el-form-item>

                <el-form-item prop="cssClass" v-show="false">
                    <el-input
                        v-model="form.cssClass"
                        placeholder="请输入样式属性"
                    />
                </el-form-item>
                <el-form-item prop="dictSort" v-show="false">
                    <el-input-number
                        v-model="form.dictSort"
                        controls-position="right"
                        :min="0"
                    />
                </el-form-item>
                <el-form-item prop="listClass" v-show="false">
                    <el-select v-model="form.listClass">
                        <el-option
                            v-for="item in listClassOptions"
                            :key="item.value"
                            :label="item.label + '(' + item.value + ')'"
                            :value="item.value"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item prop="status" v-show="false">
                    <el-radio-group v-model="form.status">
                        <el-radio
                            v-for="dict in dict.type.sys_normal_disable"
                            :key="dict.value"
                            :label="dict.value"
                            >{{ dict.label }}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="类别描述" prop="remark">
                    <el-input
                        v-model="form.remark"
                        type="textarea"
                        placeholder="请输入内容"
                        :rows="8"
                    ></el-input>
                </el-form-item>
            </el-form>
            <el-form
                v-else-if="
                    form.dictType == 'contract_termination_reason' &&
                    $store.getters.allDefectObj['HR-WEB-017'] == 0
                "
                ref="form"
                :model="form"
                :rules="rules2"
                label-width="80px"
            >
                <el-form-item>
                    <el-input
                        v-model="form.dictType"
                        :disabled="true"
                        v-show="false"
                    />
                </el-form-item>

                <el-form-item label="类别名称" prop="dictLabel">
                    <el-input
                        v-model="form.dictLabel"
                        placeholder="请输入数据标签"
                    />
                </el-form-item>

                <el-form-item prop="cssClass" v-show="false">
                    <el-input
                        v-model="form.cssClass"
                        placeholder="请输入样式属性"
                    />
                </el-form-item>
                <el-form-item prop="dictSort" v-show="false">
                    <el-input-number
                        v-model="form.dictSort"
                        controls-position="right"
                        :min="0"
                    />
                </el-form-item>
                <el-form-item prop="listClass" v-show="false">
                    <el-select v-model="form.listClass">
                        <el-option
                            v-for="item in listClassOptions"
                            :key="item.value"
                            :label="item.label + '(' + item.value + ')'"
                            :value="item.value"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item prop="status" v-show="false">
                    <el-radio-group v-model="form.status">
                        <el-radio
                            v-for="dict in dict.type.sys_normal_disable"
                            :key="dict.value"
                            :label="dict.value"
                            >{{ dict.label }}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="类别描述" prop="remark">
                    <el-input
                        v-model="form.remark"
                        type="textarea"
                        placeholder="请输入内容"
                        :rows="8"
                    ></el-input>
                </el-form-item>
            </el-form>
            <el-form
                v-else
                ref="form"
                :model="form"
                :rules="rules"
                label-width="80px"
            >
                <el-form-item>
                    <el-input
                        v-model="form.dictType"
                        :disabled="true"
                        v-show="false"
                    />
                </el-form-item>

                <el-form-item label="类别名称" prop="dictLabel">
                    <el-input
                        v-model="form.dictLabel"
                        placeholder="请输入数据标签"
                    />
                </el-form-item>

                <el-form-item prop="cssClass" v-show="false">
                    <el-input
                        v-model="form.cssClass"
                        placeholder="请输入样式属性"
                    />
                </el-form-item>
                <el-form-item prop="dictSort" v-show="false">
                    <el-input-number
                        v-model="form.dictSort"
                        controls-position="right"
                        :min="0"
                    />
                </el-form-item>
                <el-form-item prop="listClass" v-show="false">
                    <el-select v-model="form.listClass">
                        <el-option
                            v-for="item in listClassOptions"
                            :key="item.value"
                            :label="item.label + '(' + item.value + ')'"
                            :value="item.value"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item prop="status" v-show="false">
                    <el-radio-group v-model="form.status">
                        <el-radio
                            v-for="dict in dict.type.sys_normal_disable"
                            :key="dict.value"
                            :label="dict.value"
                            >{{ dict.label }}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="类别描述" prop="remark">
                    <el-input
                        v-model="form.remark"
                        type="textarea"
                        placeholder="请输入内容"
                        :rows="8"
                    ></el-input>
                </el-form-item>
            </el-form>

            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">保 存</el-button>
                <el-button @click="cancel">关 闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    listData,
    getData,
    delData,
    addData,
    updateData,
    moveData,
} from "@/api/system/dict/data";
import {
    optionselect as getDictOptionselect,
    getType,
    getTypeByType,
} from "@/api/system/dict/type";

export default {
    name: "Data",
    dicts: ["sys_normal_disable"],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示查询条件
            showSearch: true,
            // 总条数
            total: 0,
            // 字典表格数据
            dataList: [],
            // 原始老数据
            oldDataList: [],
            // 默认字典类型
            defaultDictType: "",
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 数据标签回显样式
            listClassOptions: [
                {
                    value: "default",
                    label: "默认",
                },
                {
                    value: "primary",
                    label: "主要",
                },
                {
                    value: "success",
                    label: "成功",
                },
                {
                    value: "info",
                    label: "信息",
                },
                {
                    value: "warning",
                    label: "警告",
                },
                {
                    value: "danger",
                    label: "危险",
                },
            ],
            // 类型数据字典
            typeOptions: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                dictName: undefined,
                dictType: undefined,
                status: undefined,
            },
            // 表单参数
            form: {
                remark: "",
            },
            // 表单校验
            rules: {
                dictLabel: [
                    {
                        required: true,
                        message: "数据标签不能为空",
                        trigger: "blur",
                    },
                    //不能大于50个字符
                    {
                        max: 50,
                        message: "数据标签不能大于50个字符",
                        trigger: "blur",
                    },
                ],
                remark: [
                    {
                        max: 200,
                        message: "数据描述不能大于200个字符",
                        trigger: "blur",
                    },
                ],
                dictValue: [
                    {
                        required: true,
                        message: "数据键值不能为空",
                        trigger: "blur",
                    },
                ],
                // dictSort: [
                //     {
                //         required: true,
                //         message: "数据顺序不能为空",
                //         trigger: "blur",
                //     },
                // ],
            },
            rules2: {
                dictLabel: [
                    {
                        required: true,
                        message: "数据标签不能为空",
                        trigger: "blur",
                    },
                    //不能大于50个字符
                    {
                        max: 50,
                        message: "数据标签不能大于50个字符",
                        trigger: "blur",
                    },
                ],
                remark: [
                    {
                        required: true,
                        message: "类别描述不能为空",
                        trigger: "blur",
                    },
                    {
                        max: 200,
                        message: "数据描述不能大于200个字符",
                        trigger: "blur",
                    },
                ],
                dictValue: [
                    {
                        required: true,
                        message: "数据键值不能为空",
                        trigger: "blur",
                    },
                ],
                dictSort: [
                    {
                        required: true,
                        message: "数据顺序不能为空",
                        trigger: "blur",
                    },
                ],
            },
        };
    },
    created() {
        const type = this.$route.query && this.$route.query.type;
        this.getType(type);
        this.getTypeList();
    },
    methods: {
        moveUp(row) {
            console.log(this.ids);
        },
        moveDown() {},
        /** 查询字典类型详细 */
        getType(type) {
            getTypeByType(type).then((response) => {
                this.queryParams.dictType = response.data.dictType;
                this.queryParams.dictName = response.data.dictName;
                this.defaultDictType = response.data.dictType;
                this.getList();
            });
        },
        /** 查询字典类型列表 */
        getTypeList() {
            getDictOptionselect().then((response) => {
                this.typeOptions = response.data;
            });
        },
        /** 查询字典数据列表 */
        getList() {
            this.loading = true;
            listData(this.queryParams).then((response) => {
                this.oldDataList = response.rows;
                let tempDataList = response.rows.filter((item) => {
                    return item.status == 0;
                });

                this.dataList = tempDataList;
                console.log(response);
                this.getTotal();
               
                this.loading = false;
            });
        },

        getTotal(){
            let tempParams = {...this.queryParams}
            tempParams.pageSize = 99999 
            listData(tempParams).then((response) => {
              
                let tempDataList = response.rows.filter((item) => {
                    return item.status == 0;
                });

                this.total = tempDataList.length;
 
            });
        },
        //上下移动
        move(index, flag) {
            if (flag == "up") {
                if (index == 0) {
                    this.$modal.msgWarning("当前数据为第一行，无法上移");
                    return;
                } else {
                    moveData(
                        this.dataList[index - 1].dictCode,
                        this.dataList[index].dictCode
                    ).then((response) => {
                        this.$modal.msgSuccess("上移成功");
                        this.getList();
                    });
                }
            } else {
                if (index == this.dataList.length - 1) {
                    this.$modal.msgWarning("当前数据为最后一行，无法下移");
                    return;
                } else {
                    moveData(
                        this.dataList[index + 1].dictCode,
                        this.dataList[index].dictCode
                    ).then((response) => {
                        this.$modal.msgSuccess("下移成功");
                        this.getList();
                    });
                }
            }
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            // this.form = {
            //     dictCode: undefined,
            //     dictLabel: undefined,
            //     dictValue: undefined,
            //     cssClass: undefined,
            //     listClass: "default",
            //     dictSort: 0,
            //     status: "0",
            //     remark: undefined,
            // };
            this.resetForm("form");
        },
        /** 查询按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 返回按钮操作 */
        // handleClose() {
        //   const obj = { path: "/system/dict" };
        //   this.$tab.closeOpenPage(obj);
        // },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.queryParams.dictType = this.defaultDictType;
            this.handleQuery();
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "创建类别";
            this.form.dictType = this.queryParams.dictType;
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.dictCode);
            console.log(selection);
            this.single = selection.length != 1;
            this.multiple = !selection.length;
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const dictCode = row.dictCode || this.ids;
            getData(dictCode).then((response) => {
                this.form = response.data;

                if (
                    this.form.dictType == "political_status" &&
                    this.$store.getters.allDefectObj["HR-WEB-015"] == 0
                ) {
                    this.form.dictLabel = "";
                    this.form.remark = "";
                }
                this.open = true;
                this.title = "修改类别";
            });
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    // 新增判断是不是在显示表中
                    if (this.form.dictCode == undefined) {
                        if (
                            this.form.dictType == "job_category" &&
                            this.$store.getters.allDefectObj["HR-WEB-014"] == 0
                        ) {
                        } else {
                            let tempObj2 = this.dataList.find(
                                (item) => item.dictLabel == this.form.dictLabel
                            );
                            if (tempObj2) {
                                this.$modal.msgError("类别名称已存在");
                                return;
                            }
                            //手工判断是不是存在 总列表中
                            let tempObj = this.oldDataList.find(
                                (item) => item.dictLabel == this.form.dictLabel
                            );
                            //如果存在
                            if (tempObj) {
                                //新的类别描述
                                let tempRemark = this.form.remark || "";
                                this.form = { ...tempObj };
                                // 改变状态
                                this.form.status = 0;
                                // 改变类别描述
                                this.form.remark = tempRemark;
                                //位置改为最后一个
                                this.form.dictSort =
                                    this.oldDataList[
                                        this.oldDataList.length - 1
                                    ].dictSort + 1;
                            }
                        }
                    }

                    if (this.form.dictCode != undefined) {
                        if (
                            this.$store.getters.allDefectObj["HR-WEB-018"] == 0
                        ) {
                            this.open = false;
                            return;
                        }

                        updateData(this.form).then((response) => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        this.form.dictValue = this.oldDataList.length + 1;
                        this.form.dictSort =
                            this.oldDataList.length > 0
                                ? this.oldDataList[this.oldDataList.length - 1]
                                      .dictSort + 1
                                : 1; // 自动递增,第一条默认是1
                        addData(this.form).then((response) => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const dictCodes = row.dictCode || this.ids;
            this.$modal
                .confirm('是否确认删除字典编码为"' + dictCodes + '"的数据项？')
                .then(() => {
                    // return delData(dictCodes);
                    // 改变数据状态
                    this.form = { ...row };
                    this.form.status = 1;
                    return updateData(this.form);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                })
                .catch(() => {});
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                "system/dict/data/export",
                {
                    ...this.queryParams,
                },
                `data_${new Date().getTime()}.xlsx`
            );
        },
    },
};
</script>
